package com.daxue.se.io;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * @author daxue0929
 * @date 2023/01/02
 **/
public class InputStreamTest {

    public String filePath;

    @BeforeEach
    public void init() throws IOException {
        filePath = new File("..").getCanonicalPath() + File.separator + "files";
    }


    @Test
    public void test005() throws FileNotFoundException {
        FileInputStream inputStream = new FileInputStream(filePath + File.separator + "test003.txt");
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);



    }

    @Test
    public void test004() throws FileNotFoundException {
        String word = "231";
        // 创造出来ByteArrayInputStream的方式
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(word.getBytes());

        // 传入一个InputStream 创造一个 BufferInputStream
        BufferedInputStream bufferedInputStream = new BufferedInputStream(byteArrayInputStream);

    }

    @Test
    public void test003() throws FileNotFoundException {
        FileInputStream inputStream = new FileInputStream(filePath + File.separator + "test003.txt");
        DataInputStream dataInputStream = new DataInputStream(inputStream);


    }

    @Test
    public void test002() throws FileNotFoundException {
        FileInputStream inputStream = new FileInputStream(filePath + File.separator + "test003.txt");
        FileChannel channel = inputStream.getChannel(); // 从文件输入流 获取一个通道.


    }


    @Test
    public void test001() throws IOException {
        FileInputStream inputStream = new FileInputStream(filePath + File.separator + "test003.txt");
        byte[] bytes = new byte[1024];
        StringBuffer result = new StringBuffer();
        int size = 0;
        while ((size = inputStream.read(bytes)) != -1) {
            result.append(new String(bytes, 0, size));
        }
        String s = result.toString();
        System.out.println(s);
    }

}
